package com.asiainfo.demo.dao.proxy;

import com.asiainfo.demo.dao.dbcon.IYogaCon;
import com.asiainfo.demo.dao.idao.iBind;
import com.asiainfo.demo.dao.roles.Bind;
import com.asiainfo.demo.dao.impl.BindImpl;

import java.util.ArrayList;

public class BindProxy implements iBind {
    private IYogaCon con = null;
    private iBind bindDao = null;

    public BindProxy() throws Exception {
        con = new IYogaCon();
        bindDao = new BindImpl(con.getConnection());
    }

    @Override
    public boolean insertBind(Bind bind) throws Exception {
        boolean flag = false;

        if (bindDao.findBindByBindID(bind.getBindID()) == null) {
            try {
                flag = bindDao.insertBind(bind);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Primary Key Exists. Insert Failed.");
        }

        con.close();
        return flag;
    }

    @Override
    public Bind findBindByBindID(int bindID) throws Exception {
        Bind bind = bindDao.findBindByBindID(bindID);
        con.close();
        return bind;
    }

    @Override
    public ArrayList<Bind> findBindsByID(int ID, String type) throws Exception {
        ArrayList<Bind> binds = bindDao.findBindsByID(ID, type);
        con.close();

        return binds;
    }

    @Override
    public ArrayList<Bind> findAllBind() throws Exception {
        ArrayList<Bind> binds = bindDao.findAllBind();
        con.close();

        return binds;
    }

    @Override
    public boolean changeValid(int bindID, byte newStatus) throws Exception {
        boolean flag = false;

        if (bindDao.findBindByBindID(bindID) != null) {
            try {
                flag = bindDao.changeValid(bindID, newStatus);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        con.close();

        return flag;
    }
}
